﻿using AutoMapper;
using Entity.BaseInfo.DTO.Department;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Department;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class DepartmentService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public DepartmentService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取部门列表
         */
        public async Task<RequestResponseModel<PageResult<DepartmentView>>> GetDepartmentList(DepartmentSearchDTO departmentSearchDTO)
        {
            //筛选
            var query = _context.DepartmentView
                .WhereIF(a => a.Code.Contains(departmentSearchDTO.Code), !string.IsNullOrEmpty(departmentSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(departmentSearchDTO.Name), !string.IsNullOrEmpty(departmentSearchDTO.Name))
                ;

            //返回列表
            var departmentList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(departmentSearchDTO.CurrentPage, departmentSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<DepartmentView>(departmentList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增部门
         */
        public async Task<RequestResponseModel<string>> AddDepartment(DepartmentAddDTO departmentAddDTO)
        {
            //新增数据
            var department = _mapper.Map<DepartmentModel>(departmentAddDTO);
            department.CreateTime = DateTime.Now;
            department.Id = Guid.NewGuid().ToString();

            //更新表
            _context.Department.Add(department);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", department.Id);
        }

        /**
         * 更新部门
         */
        public async Task<RequestResponseModel<string>> PutDepartment(DepartmentUpdateDTO departmentUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<DepartmentModel>(_context.Department, departmentUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", departmentUpdateDTO.Id);
            };

            //编辑数据
            var department = _mapper.Map(departmentUpdateDTO, query);

            //更新表
            _context.Department.Update(department);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", department.Id);
        }

        /**
         * 删除部门
         */
        public async Task<RequestResponseModel<string>> DeleteDepartment(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<DepartmentModel>(_context.Department, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //查询部门下员工
            var staffList = await _context.Staff
                .Where(a => a.DepartmentId == Id)
                .ToListAsync()
                ;

            //判断员工
            if (staffList.Count > 0)
            {
                foreach (var staff in staffList)
                {
                    staff.DepartmentId = null;
                }
            }

            //更新表
            _context.Department.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 下拉部门
         */
        public async Task<RequestResponseModel<List<DepartmentSelectView>>> GetDepartmentSelectList(DepartmentSelectSearchDTO departmentSelectSearchDTO)
        {
            //筛选
            var query = await _context.DepartmentSelectView
                .WhereIF(a => a.Code.Contains(departmentSelectSearchDTO.KeyWord) || a.Name.Contains(departmentSelectSearchDTO.KeyWord), !string.IsNullOrEmpty(departmentSelectSearchDTO.KeyWord))
                .ToListAsync();

            //返回数据
            return RequestResponse.CreateResponse(200, "查询成功", query);
        }

    }
}
